package org.bjou

import org.springframework.dao.DataIntegrityViolationException

class UserAttachController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def uploadCourseRule = {
       def files = request.getFileMap();
       //handle uploaded file
        def uploadedFile = request.getFile('fileupload')
    
      if(!uploadedFile.empty){
      println "Class: ${uploadedFile.class}"
      println "Name: ${uploadedFile.name}"
      println "OriginalFileName: ${uploadedFile.originalFilename}"
      println "Size: ${uploadedFile.size}"
      println "ContentType: ${uploadedFile.contentType}"
      println "currentTerm: ${params.entryTerm}"

      def webRootDir = servletContext.getRealPath("/")
      def userDir = new File(webRootDir, "/upload")
      userDir.mkdirs()
      uploadedFile.transferTo(new File( userDir, uploadedFile.originalFilename))
      //println userDir
      
      
      def fileUrl = "upload/" + uploadedFile.originalFilename.toString()
      
       def userAttachInstance = new UserAttach(params)
       userAttachInstance.fileUrl = fileUrl
       userAttachInstance.fileName = uploadedFile.originalFilename
       if (!userAttachInstance.save(flush: true)) {
            render(view: "create", model: [userAttachInstance: userAttachInstance])
            return
        }
       redirect(controller: "UserBaseinfo", action: "edit", id: userAttachInstance.userBaseinfo.id, fragment:"tabs-4")

         }
        	 
    }
    
    
    
    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [userAttachInstanceList: UserAttach.list(params), userAttachInstanceTotal: UserAttach.count()]
    }

    def create() {
        [userAttachInstance: new UserAttach(params)]
    }

    def save() {
        def userAttachInstance = new UserAttach(params)
        if (!userAttachInstance.save(flush: true)) {
            render(view: "create", model: [userAttachInstance: userAttachInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'userAttach.label', default: 'UserAttach'), userAttachInstance.id])
        redirect(action: "show", id: userAttachInstance.id)
    }

    def show() {
        def userAttachInstance = UserAttach.get(params.id)
        if (!userAttachInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'userAttach.label', default: 'UserAttach'), params.id])
            redirect(action: "list")
            return
        }

        [userAttachInstance: userAttachInstance]
    }

    def edit() {
        def userAttachInstance = UserAttach.get(params.id)
        if (!userAttachInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'userAttach.label', default: 'UserAttach'), params.id])
            redirect(action: "list")
            return
        }

        [userAttachInstance: userAttachInstance]
    }

    def update() {
        def userAttachInstance = UserAttach.get(params.id)
        if (!userAttachInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'userAttach.label', default: 'UserAttach'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (userAttachInstance.version > version) {
                userAttachInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'userAttach.label', default: 'UserAttach')] as Object[],
                          "Another user has updated this UserAttach while you were editing")
                render(view: "edit", model: [userAttachInstance: userAttachInstance])
                return
            }
        }

        userAttachInstance.properties = params

        if (!userAttachInstance.save(flush: true)) {
            render(view: "edit", model: [userAttachInstance: userAttachInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'userAttach.label', default: 'UserAttach'), userAttachInstance.id])
        redirect(action: "show", id: userAttachInstance.id)
    }

    def delete() {
        def userAttachInstance = UserAttach.get(params.id)
        if (!userAttachInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'userAttach.label', default: 'UserAttach'), params.id])
            redirect(action: "list")
            return
        }

        try {
            userAttachInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'userAttach.label', default: 'UserAttach'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'userAttach.label', default: 'UserAttach'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
